import request from '../utils/request'
import querystring from 'querystring' // 序列化请求参数

let formatData = (data) => {
  return querystring.stringify(data)
}// 序列化请求参数 封装函数

const URL = {
  test: 'wx/test',
  getBlog: 'findAllBlogAndCommit',
  saveBlog: 'addBlog',
  findBlogByBlogId: 'findBlogByBlogId',
  upImg: 'upImg',
  findBlogCommitByBlogId: 'findBlogCommitByBlogId',
  addCommit: 'addCommit',
  findBlogClass: 'findBlogClass',
  findBlogByUsername: 'findBlogByUsername',
  updateBlog: 'updateBlog',
  addFabulous: 'addFabulous',
  cancelFabulous: 'cancelFabulous',
  addReadCount: 'addReadCount',
  changeHeadImg: 'changeHeadImg',
  updatedUserInfo: 'updatedUserInfo',
  findAllBlogByclassName: 'findAllBlogByclassName',
  findFabByUsername: 'findFabByUsername'
  // 必应随机图片接口https://uploadbeta.com/api/pictures/random/?key=BingEverydayWallpaperPicture
}

export default {
  test () {
    return request(URL.test, 'post', {name: 'xlj', password: '12345'})
  },
  getBlog ({page, row}) {
    return request(URL.getBlog, 'get', {page, row})
  },
  findAllBlogByclassName ({className, page, row}) {
    return request(URL.findAllBlogByclassName, 'get', {className, page, row})
  },
  saveBlog (name, title, body, className) {
    return request(URL.saveBlog, 'post', formatData({name, title, body, className}), false)
  },
  findBlogByBlogId (blogId) {
    return request(URL.findBlogByBlogId, 'get', {blogId})
  },
  upImg (imgData) {
    return request(URL.upImg, 'post', formatData({imgData}))
  },
  findBlogCommitByBlogId (blogId) {
    return request(URL.findBlogCommitByBlogId, 'get', {blogId})
  },
  addCommit (commitName, blogId, commitBody, avatar) {
    return request(URL.addCommit, 'post', formatData({commitName, blogId, commitBody, avatar}), false)
  },
  findBlogClass () {
    return request(URL.findBlogClass, 'get')
  }
}
